package com.example.pattern.builder;

/**
 * 建造者模式
 */
public class BuilderTest {

    public static void main(String[] args) {
        ProductBuilder productBuilder = new DefaultProduct("acai", 25, "earth");
        System.out.println(productBuilder.builder());

        ProductBuilder specialProduct = new SpecialProduct("acai", 25, "earth");
        specialProduct.setName("stephen");
        System.out.println(specialProduct);
    }

}

interface ProductBuilder{
    void setName(String name);
    void setAge(int age);
    void setAddress(String address);
    ProductBuilder builder();
}
class DefaultProduct implements ProductBuilder{

    private String name;
    private int age;
    private String address;

    public DefaultProduct(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public ProductBuilder builder() {
        return new DefaultProduct(this.name, this.age, this.address);
    }

    @Override
    public String toString() {
        return "DefaultProduct{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}
class SpecialProduct implements ProductBuilder{

    private String name;
    private int age;
    private String address;

    public SpecialProduct(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public ProductBuilder builder() {
        return new SpecialProduct(this.name, this.age, this.address);
    }
}